package com.elitel.frame.base.service.strategy.service;

import com.elitel.frame.main.dao.polym.CfgMultQueryFieldMapper;
import com.elitel.frame.main.dao.polym.CfgMultRelationFieldMapper;
import com.elitel.frame.main.dao.polym.CfgMultRelationMapper;
import com.elitel.frame.main.dao.polym.CfgMultResultFieldMapper;
import com.elitel.frame.main.entity.enums.BuildServiceEnum;
import com.elitel.frame.main.entity.ext.PrjServiceExt;
import com.elitel.frame.main.entity.polym.CfgMultQueryField;
import com.elitel.frame.main.entity.polym.CfgMultRelation;
import com.elitel.frame.main.entity.polym.CfgMultRelationField;
import com.elitel.frame.main.entity.polym.CfgMultResultField;
import com.elitel.frame.main.entity.vo.ServiceConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 单表服务策略
 *
 * @author xqh
 * @date 2025/02/07
 */
@Component
@Slf4j
public class PolymerServerStrategy extends BuildServiceStrategy {

    @Resource
    private CfgMultRelationMapper cfgMultRelationMapper;

    @Resource
    private CfgMultRelationFieldMapper cfgMultRelationFieldMapper;

    @Resource
    private CfgMultQueryFieldMapper cfgMultQueryFieldMapper;

    @Resource
    private CfgMultResultFieldMapper cfgMultResultFieldMapper;

    /**
     * 获取策略
     *
     * @return 策略枚举
     */
    protected BuildServiceEnum getTypeEnum() {
        return BuildServiceEnum.polymerServer;
    }

    @Override
    public List<ServiceConfig> buildServiceModel(List<PrjServiceExt> prjServiceExtList) {

        // 收集所有需要处理的serviceId
        List<String> serviceGuids = prjServiceExtList.stream()
                .map(PrjServiceExt::getGuid)
                .collect(Collectors.toList());

        //查询聚合关联信息
        List<CfgMultRelation> cfgMultRelationList = cfgMultRelationMapper.selectByServiceId(serviceGuids);
        Map<String, CfgMultRelation> cfgMultRelationMap = cfgMultRelationList.stream()
                .collect(Collectors.toMap(CfgMultRelation::getServiceId, Function.identity()));
        //查询聚合关联字段信息
        List<CfgMultRelationField> cfgMultRelationFieldList = cfgMultRelationFieldMapper.selectByServiceId(serviceGuids);
        Map<String, List<CfgMultRelationField>> cfgMultRelationFieldMap = cfgMultRelationFieldList.stream().collect(Collectors.groupingBy(CfgMultRelationField::getServiceId));
        //查询聚合查询字段信息
        List<CfgMultQueryField> cfgMultQueryFields = cfgMultQueryFieldMapper.selectByServiceId(serviceGuids);
        Map<String, List<CfgMultQueryField>> cfgMultQueryFieldMap = cfgMultQueryFields.stream().collect(Collectors.groupingBy(CfgMultQueryField::getServiceId));
        //查询聚合显示字段信息
        List<CfgMultResultField> cfgMultResultFieldList = cfgMultResultFieldMapper.selectByServiceId(serviceGuids);
        Map<String, List<CfgMultResultField>> cfgMultResultFieldMap = cfgMultResultFieldList.stream().collect(Collectors.groupingBy(CfgMultResultField::getServiceId));

        List<ServiceConfig> result = new ArrayList<>();

        for (PrjServiceExt service : prjServiceExtList) {

            ServiceConfig serviceConfig = getBaseServiceMap(service);

            String guid = service.getGuid();
            CfgMultRelation cfgMultRelation = cfgMultRelationMap.get(guid);
            ServiceConfig.PolymerInfo polymerInfo = new ServiceConfig.PolymerInfo();
            polymerInfo.setServiceId1(cfgMultRelation.getFirstServiceId());
            polymerInfo.setServiceId2(cfgMultRelation.getSecondServiceId());
            polymerInfo.setCondition(cfgMultRelation.getCondition());

            List<CfgMultRelationField> cfgMultRelationFields = cfgMultRelationFieldMap.get(guid);
            if (!CollectionUtils.isEmpty(cfgMultRelationFields)) {
                List<ServiceConfig.RelationField> relationFieldList = cfgMultRelationFields.stream().map(item -> {
                    ServiceConfig.RelationField relationField = new ServiceConfig.RelationField();
                    relationField.setFieldCode1(item.getFirstFieldCode());
                    relationField.setFieldCode2(item.getSecondFieldCode());
                    relationField.setCondition(item.getCondition());
                    return relationField;
                }).collect(Collectors.toList());
                polymerInfo.setRelationField(relationFieldList);
            }

            List<CfgMultQueryField> cfgMultQueryFieldList = cfgMultQueryFieldMap.get(guid);
            if (!CollectionUtils.isEmpty(cfgMultQueryFieldList)) {
                List<ServiceConfig.QueryField> queryFieldList = cfgMultQueryFieldList.stream().map(item -> {
                    ServiceConfig.QueryField queryField = new ServiceConfig.QueryField();
                    queryField.setSourceServiceId(item.getSourceServiceId());
                    queryField.setSourceFieldCode(item.getSourceFieldCode());
                    queryField.setNewFieldCode(item.getNewFieldCode());
                    return queryField;
                }).collect(Collectors.toList());
                polymerInfo.setQueryField(queryFieldList);
            }

            List<CfgMultResultField> cfgMultResultFields = cfgMultResultFieldMap.get(guid);
            if (!CollectionUtils.isEmpty(cfgMultResultFields)) {
                List<ServiceConfig.ResultField> resultFieldList = cfgMultResultFields.stream().map(item -> {
                    ServiceConfig.ResultField resultField = new ServiceConfig.ResultField();
                    resultField.setSourceServiceId(item.getSourceServiceId());
                    resultField.setSourceFieldCode(item.getSourceFieldCode());
                    resultField.setNewFieldCode(item.getNewFieldCode());
                    resultField.setCondition(item.getCondition());
                    return resultField;
                }).collect(Collectors.toList());
                polymerInfo.setResultField(resultFieldList);
            }

            ServiceConfig.ExtInfo extInfoMap = new ServiceConfig.ExtInfo();

            extInfoMap.setPolymerInfo(polymerInfo);

            serviceConfig.setExtInfo(extInfoMap);
            result.add(serviceConfig);
        }

        return result;
    }


}
